﻿Imports Windows.UI

Namespace Controls
    Public Class GradientButton
        Inherits Button

        Public Shared ReadOnly Color1Property As DependencyProperty = DependencyProperty.Register("Color1", GetType(Color), GetType(GradientButton), New PropertyMetadata(Colors.Red, AddressOf OnColorChanged))
        Public Shared ReadOnly Color2Property As DependencyProperty = DependencyProperty.Register("Color2", GetType(Color), GetType(GradientButton), New PropertyMetadata(Colors.Green, AddressOf OnColorChanged))

        Dim gradientStop1, gradientStop2 As GradientStop

        Public Sub New()
            Dim brush As New LinearGradientBrush
            brush.StartPoint = New Point(0, 0)
            brush.EndPoint = New Point(1, 0)
            gradientStop1 = New GradientStop
            gradientStop1.Offset = 0
            gradientStop1.Color = Color1
            brush.GradientStops.Add(gradientStop1)
            gradientStop2 = New GradientStop
            gradientStop2.Offset = 1
            gradientStop2.Color = Color2
            brush.GradientStops.Add(gradientStop2)
            Foreground = brush
        End Sub

        Shared Sub OnColorChanged(obj As DependencyObject, args As DependencyPropertyChangedEventArgs)
            Dim button = DirectCast(obj, GradientButton)
            If args.Property Is Color1Property Then
                button.gradientStop1.Color = CType(args.NewValue, Color)
            End If
            If args.Property Is Color2Property Then
                button.gradientStop2.Color = CType(args.NewValue, Color)
            End If
        End Sub

        Public Property Color1 As Color
            Get
                Return CType(GetValue(Color1Property), Color)
            End Get
            Set(value As Color)
                SetValue(Color1Property, value)
            End Set
        End Property

        Public Property Color2 As Color
            Get
                Return CType(GetValue(Color2Property), Color)
            End Get
            Set(value As Color)
                SetValue(Color2Property, value)
            End Set
        End Property
    End Class
End Namespace